<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="90px">
            <el-form-item label="类别" prop="state" :rules="[formRulesMixin.notNull]">
                <el-select v-model="obj.data.state" placeholder="类别" class="search_select" clearable>
                    <el-option v-for="(item,index) in globalData.houseTypeList" :key="index" :label="item.text" :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="联系人" prop="man" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.man" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.mobile" clearable autocomplete="off" maxlength="11" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="memo" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.memo" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="起始时间" prop="starttime">
                <DatePicker v-model:value="obj.data.starttime" />
            </el-form-item>
            <el-form-item label="截止时间" prop="endtime">
                <DatePicker v-model:value="obj.data.endtime" />
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import DatePicker from '@/components/date_picker.vue'
    const emit = defineEmits(['editComplete','close']) //此处要完全对应，采用固定的update:prop属性
    const props = defineProps({
        detail_id: { type: String },
        house_id: { type: String }
    })

    let { proxy } = getCurrentInstance()
    let obj = reactive({
        data:{
            detail_id: props.detail_id,
            house_id: props.house_id,
            state: "",
            man: "",
            mobile:"",
            starttime: "",
            endtime:"",
            memo:"",
        },
    })

    let init = async () => {
        if(obj.data.detail_id > 0){
            proxy.$ajax({
                url: 'house/history_get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                    obj.data.state = parseInt(obj.data.state)
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'house/history_design',
                    data: obj.data,
                    bfLoading: true,
                    // timeout:60000
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.detail_id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('editComplete')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>